<template>
  <div>
    <avue-form ref="form" v-model="form" :option="option"></avue-form>
    <div class="bottom-menu" v-if="!isView">
      <el-button
        type="primary"
        size="small"
        :disabled="btn"
        @click="handelSubmit"
        :loading="isSubmit"
        >保 存</el-button
      >
      <el-button size="small" @click="handleCancle" :loading="isSubmit"
        >取 消</el-button
      >
    </div>
  </div>
</template>

<script>
import {
  fetchList,
  addObj,
  putObj,
} from "@/api/subwaystm/swstinputpassengerwork/special-work";
import {validateDeviceOperateNo} from "@/util/validate";
export default {
  name: "special-work",
  inject: ["passengerWork"],
  data() {
    return {
      form: {},
      relationId: null,
      isSubmit: false,
      btn: false,
      option: {
        labelWidth: "55%",
        menuBtn: false,
        column: [
          {
            label: "现役军人、武警、消防员发放张数",
            prop: "soliderNumber",
            type: "number",
          },
          {
            label: "离退休干部士官、部分优抚对象发放张数",
            prop: "f1",
            type: "number",
          },
	  // {
    //         label: "现役军人、武警、消防员专窗张数",
    //         prop: "f3",
    //         type: "number",
    //       },
          {
            label: "残疾人等发放张数",
            prop: "f2",
            type: "number",
          },
          {
            label: "其他情况发放张数",
            prop: "otherNumber",
            type: "number",
          },
          {
            label: "合计销售张数",
            prop: "sellNumber",
            type: "number",
            disabled: true,
          },
          // {
          //   label: "上传截图",
          //   prop: "filePath",
          //   type: "upload",
          //   listType: "picture-img",
          //   component: "zd-upload",
          // },
        ],
      },
    };
  },
  computed: {
    isView() {
      return this.boxType == "view";
    },
  },
  props: {
    workInfo: Object,
    boxType: String,
  },
  watch: {
    form: {
      handler(newValue, oldValue) {
        newValue.sellNumber =
          (newValue.soliderNumber || 0) + (newValue.otherNumber || 0) + (newValue.f1 || 0)+ (newValue.f2 || 0)+ (newValue.f3 || 0);
      },
      immediate: true,
      deep: true,
    },
  },
  methods: {
    getList() {
      if (this.relationId != this.workInfo.id) {
        this.form = {};
        fetchList(this.workInfo.id).then((response) => {
          this.relationId = this.workInfo.id;
          this.form = response.data.data || {};
        });
      }
    },
    clear() {
      this.form = {};
    },
    handelSubmit() {
      if (this.form.soliderNumber == null && this.form.f1 == null && this.form.f2 == null && this.form.f3 == null && this.form.otherNumber == null) {
        this.$message.info("请填写现役军人、武警、消防员发放张数，离退休干部士官、部分优抚对象发放张数，现役军人、武警、消防员专窗张数，残疾人等发放张数，或者其他情况发放张数！");
        return;
      }
      this.form.sellNumber =
      (this.form.soliderNumber || 0) + (this.form.otherNumber || 0) + (this.form.f1 || 0)+ (this.form.f2 || 0)+ (this.form.f3 || 0);

      var params = {
        ...this.workInfo,
        workList: [this.form],
      };
      if (this.boxType == "add") {
        this.passengerWork.validateForm((valid) => {
          if (valid) {
            if (!validateDeviceOperateNo(this.workInfo.deviceOperateNum)) {
              this.$message.error("设备操作号必须为09开头的6位纯数字！");
              return;
            }
            this.$refs.form.validate((valid) => {
              if (valid) {
                this.isSubmit = true;
                addObj(params).then((response) => {
                  this.$message.success("添加成功");
                  this.isSubmit = false;
                  this.btn = true
		  this.passengerWork.getList(this.passengerWork.page)//20240827 测试  点击保存成功后刷新主页面
                });
              }
            });
          }
        });
      } else {
        this.passengerWork.validateForm((valid) => {
          if (valid) {
            this.$refs.form.validate((valid) => {
              if (valid) {
                this.isSubmit = true;
                putObj(params).then((response) => {
                  this.$message.success("修改成功");
                  this.isSubmit = false;
                  this.btn = true
                });
              }
            });
          }
        });
      }
    },
    handleCancle() {
        if(this.boxType == 'mergeEdit'){
          this.passengerWork.closeDialogMerge()
        }else{
          this.passengerWork.closeDialog()
        }
    },
  },
};
</script>

<style lang="scss" scoped>
.bottom-menu {
  margin-top: 30px;
  display: flex;
  justify-content: center;
}
</style>
